﻿namespace WPFDBBrowser.Framework
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
using System.ComponentModel;

    public class WindowBase<TPresenter, TView> : Window
        where TPresenter : PresenterBase<TView>, new()
    {
        public WindowBase()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                if (!(this is TView))
                    throw new InvalidCastException(
                        string.Format("Can't cast window to {0}.", typeof(TView).FullName));
                this.Presenter = new TPresenter();
                this.Presenter.SetView(this);
                this.Loaded += new RoutedEventHandler(WindowBase_Loaded);
                this.Closed += new EventHandler(WindowBase_Closed);
            }
            this.BaseInitialized();
        }

        void WindowBase_Closed(object sender, EventArgs e)
        {
            this.Presenter.OnViewClosed();
        }

        void WindowBase_Loaded(object sender, RoutedEventArgs e)
        {
            this.Presenter.OnViewLoaded();
        }
        
        protected TPresenter Presenter { get; private set; }
        
        protected virtual void BaseInitialized()
        {
        }
    }
}
